package com.jni.ws;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

public class DownloadImagesTask extends AsyncTask<View, Void, Bitmap> {

ProgressBar p = null;
View vue = null;

public DownloadImagesTask(ProgressBar pbr) {
this.p = pbr;

}

public DownloadImagesTask() {

}

@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(Bitmap result) {
Drawable drawable = new BitmapDrawable(result);
vue.setBackgroundDrawable(drawable);
Log.d("DownloadImagesTask", "setting the image");
// imageView.setImageBitmap(result);
// mcv.pBarChannel.setVisibility(View.GONE);
if (p != null) {
p.setVisibility(View.GONE);
}
}

private Bitmap download_Image(String url) {
// -------------------------------------------------
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("Hub", "Error getting the image from server : "
+ e.getMessage().toString());
}
return bm;
}

@Override
protected Bitmap doInBackground(View... params) {
this.vue = (View) params[0];

return download_Image((String) vue.getTag());
}

protected void onPreExecute() {
super.onPreExecute();

}

protected void onProgressUpdate(String... progress) {

}
}